{createAction} = require('redux-actions')
{fetchGetMenu,getMenuFail} = require('./menuInfo.js')
Toast = require('antd-mobile/lib/toast')

actions =
  guestLoginStart:createAction('USER_INFO/GUEST_LOGIN_START')
  guestLoginCompleted:createAction('USER_INFO/GUEST_LOGIN_COMPLETED')

  loginStart:createAction('USER_INFO/LOGIN_START')
  loginCompleted:createAction('USER_INFO/LOGIN_COMPLETED')
  loginFail:createAction('USER_INFO/LOGIN_FAIL')
  logOut:createAction('USER_INFO/LOGOUT')

  changePassStart:createAction('USER_INFO/CHANGE_PASS_START')
  changePassCompleted:createAction('USER_INFO/CHANGE_PASS_COMPLETED')
  changePassFail:createAction('USER_INFO/CHANGE_PASS_FAIL')
  fetchLogOut:->
    (dispatch)->
      dispatch actions.logOut()
      dispatch getMenuFail()
  fetchLogin:(loginInfo)->
    {name,pass} = loginInfo
    (dispatch)->
      dispatch actions.loginStart()
      if name is 'guest'
        dispatch actions.loginCompleted({pilotCode:'guest',pilotName:'游客',token:''})
        dispatch getMenuFail()
      else
        str = "http://202.136.214.173/api/Login?name=#{name}&pass=#{pass}"
        # str = "http://172.16.46.161/api/Login?name=#{name}&pass=#{pass}"
        fetch str,
          {
            method:'GET'
            mode:'cors'#same-origin no-cors
            cache:'default'
          }
        .then (res)->
          debugger
          if res.ok
            res.json()
          else if res.status is 500
              Promise.reject new Error('用户名或密码错误')
          else
              Promise.reject new Error('网络请求失败,请重试')
        ,(err)->
          Promise.reject new Error('好像断网了哦')
        .then (json)->
                dispatch actions.loginCompleted(Object.assign json,{pass:pass})
                dispatch fetchGetMenu(json.pilotCode,json.token)
        .catch (err)->
            debugger
            Toast.fail err.message,->
              dispatch actions.loginFail(err)
              dispatch getMenuFail()


  fetchChangePass:(loginInfo)->
      {name,oldPwd,newPwd,token} = loginInfo
      (dispatch)->
          dispatch actions.changePassStart()
          str = "http://202.136.214.173/api/ChangePass?name=#{name}&oldPwd=#{oldPwd}&newPwd=#{newPwd}"
          # str = "http://172.16.46.161/api/Login?name=#{name}&pass=#{pass}"
          fetch str,
            {
              method:'POST'
              mode:'cors'#same-origin no-cors
              cache:'default'
              headers: {
                token:token
              }
            }
          .then (res)->
            debugger
            if res.ok
              res.json()
            else if res.status is 500
                Promise.reject new Error('用户名或密码错误')
            else
                Promise.reject new Error('网络请求失败,请重试')
          ,(err)->
            Promise.reject new Error('好像断网了哦')
          .then (json)->
            if json.code is '1'
                  dispatch actions.changePassCompleted
                    name:name
                    pass:newPwd
                  dispatch getMenuFail()
            else
              Promise.reject new Error('密码修改失败！')
          .catch (err)->
              Toast.fail err.message,->
                dispatch actions.changePassFail(err)
                dispatch getMenuFail()

module.exports = actions
